package com.cgw.game.utils;

import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet;
import com.github.mikephil.charting.listener.BarLineChartTouchListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Description TODO
 * @Author YF
 * @Date 2020/12/4
 * @Version 1.0
 */
public class MyBarLineChartTouchListener extends BarLineChartTouchListener {
    public MyBarLineChartTouchListener(BarLineChartBase<? extends BarLineScatterCandleBubbleData<? extends IBarLineScatterCandleBubbleDataSet<? extends Entry>>> chart, Matrix touchMatrix, float dragTriggerDistance) {
        super(chart, touchMatrix, dragTriggerDistance);
    }
    private boolean cancel = false;
    private float lastLeftX;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                float leftX = mChart.getLowestVisibleX();
                if(lastLeftX!=leftX){
                    lastLeftX = leftX;
                    int x = Math.round(mChart.getXChartMin());
                    if (Math.round(leftX) == x&&!cancel) {//滑到最左端
                        cancel = true;
                        Log.e("mChart","滑到最左端");
                        int start = (int)lastLeftX-10;
                        List<BarEntry> barValue = new ArrayList<>();
                        for (int i = start; i < lastLeftX; i++) {
                            barValue.add(new BarEntry(i, new Random().nextInt(100)+1));
                        }
                        BarDataSet barDataSet;
                        if (mChart.getData() != null &&
                                mChart.getData().getDataSetCount() > 0) {
                            barDataSet = (BarDataSet) mChart.getData().getDataSetByIndex(0);
                            barDataSet.getValues().addAll(0,barValue);
                            mChart.notifyDataSetChanged();
                            mChart.setVisibleXRangeMaximum(8);
                            mChart.moveViewToX(lastLeftX);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                cancel = false;
                break;
        }
        return super.onTouch(v, event);
    }
}
